package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HLJ
 */
@RestController
@RequestMapping("/food")
public class FoodController {
    @Reference
    private FoodService foodService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile) throws IOException {
        List<String[]> list = POIUtils.readExcel(excelFile);
        List<Food> foods = new ArrayList<>();
        if (list != null && list.size()>0){
            for (String[] strings : list) {
                Integer id = Integer.valueOf(strings[0].trim());
                Integer foodId = Integer.valueOf(strings[1].trim());
                String foodName = strings[2];
                String foodType = strings[3];
                String foodTip = strings[4];
                float foodWater = Float.valueOf(strings[5]);
                Integer foodKcal = Integer.valueOf(strings[6].trim());
                float foodKJ = Float.valueOf(strings[7]);
                float foodProtein = Float.valueOf(strings[8]);
                float foodFat = Float.valueOf(strings[9]);
                Integer foodWeight = Integer.valueOf(strings[10].trim());
                String foodUnit = strings[11];
                Food food = new Food(id,foodId,foodName,foodType,foodTip,foodWater,foodKcal,foodKJ,foodProtein,foodFat,foodWeight,foodUnit);
                foods.add(food);
            }
            foodService.upload(foods);
        }

        return null;
    }

    @RequestMapping("/findPage")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult page = foodService.findPage(queryPageBean);
        return page;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Food food = foodService.findById(id);
        return new Result(true,MessageConstant.QUERY_FOOD_SUCCESS,food);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Food food){
        try {
            foodService.add(food);
            return new Result(true, MessageConstant.ADD_FOOD_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_FOOD_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Food food){
        try {
            foodService.edit(food);
            return new Result(true,MessageConstant.EDIT_FOOD_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_FOOD_FAIL);
        }
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            foodService.delete(id);
            return new Result(true,MessageConstant.DELETE_FOOD_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_FOOD_FAIL);
        }

    }
}
